package com.xywh.emergency_outbound.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xw.repo.XEditText;
import com.xywh.emergency_outbound.R;
import com.xywh.emergency_outbound.di.component.DaggerSettingComponent;
import com.xywh.emergency_outbound.di.module.SettingModule;
import com.xywh.emergency_outbound.mvp.contract.SettingContract;
import com.xywh.emergency_outbound.mvp.presenter.SettingPresenter;
import com.xywh.emergency_outbound.mvp.ui.entity.Setting;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.retrofiturlmanager.RetrofitUrlManager;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_SETTING)
public class SettingActivity extends BaseActivity<SettingPresenter> implements SettingContract.View {

    @BindView(R.id.xet_Ip)
    XEditText xetIp;
    @BindView(R.id.xet_Port)
    XEditText xetPort;
    @BindView(R.id.cb_jzmm)
    CheckBox cbJzmm;
    @BindView(R.id.btnSave)
    Button btnSave;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSettingComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .settingModule(new SettingModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_setting; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_setting));
//        getSettingData();
        if (!TextUtils.isEmpty(mSharedPreferencesHelper.getString(BaseConstants.IP)) && !TextUtils.isEmpty(mSharedPreferencesHelper.getString(BaseConstants.PORT))) {
            xetIp.setText(mSharedPreferencesHelper.getString(BaseConstants.IP));
            xetPort.setText(mSharedPreferencesHelper.getString(BaseConstants.PORT));
        }
    }

    @OnClick(R.id.btnSave)
    public void onViewClicked() {

        if (TextUtils.isEmpty(getIP())) {
            showMessage("请输入IP");
            return;
        }
        if (TextUtils.isEmpty(getPort())) {
            showMessage("请输入端口号");
            return;
        }

        mSharedPreferencesHelper.putString(BaseConstants.IP, getIP());
        mSharedPreferencesHelper.putString(BaseConstants.PORT, getPort());

        RetrofitUrlManager.getInstance().setGlobalDomain(getUrl());

        showMessage("设置成功");
        killMyself();
    }

    @NotNull
    private String getIP() {
        return xetIp.getText().toString().trim();
    }

    @NotNull
    private String getPort() {
        return xetPort.getText().toString().trim();
    }

    private void getSettingData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getSettingData(map);
    }

    @Override
    public void getSettingDataSuccess(Setting entityData) {


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
